package com.boful.common.date.utils;

public class TimeLengthUtils {
	/**
	 * 转换成秒
	 * 
	 * @param src
	 *            源字符串(格式：HH:MM:SS);
	 * @return
	 */
	public static long StringToNumber(String src) {
		String[] values = src.split(":");
		long hour = 0;
		long minute = 0;
		long seconds = 0;
		if (values.length == 3) {
			hour = Integer.parseInt(values[0]) * 60 * 60;
			minute = Integer.parseInt(values[1]) * 60;
			seconds = (long) Double.parseDouble(values[2]);
		} else if (values.length == 2) {
			minute = Integer.parseInt(values[0]) * 60;
			seconds = (long) Double.parseDouble(values[1]);
		} else if (values.length == 1) {
			seconds = (long) Double.parseDouble(values[0]);
		}

		return (hour + minute + seconds);
	}

	public static String NumberToString(long time) {
		int hour = (int) (time / (60 * 60));
		int minute = (int) (time % (60 * 60) / 60);
		int second = (int) (time - hour * 60 * 60 - minute * 60);
		return fixTimeNum(hour) + ":" + fixTimeNum(minute) + ":" + fixTimeNum(second);
	}

	public static String fixTimeNum(int n) {
		return n < 10 ? ("0" + n) : n + "";
	}

	public static String formatTime(String time) {
		String[] values = time.split(":");
		long hour = 0;
		long minute = 0;
		long seconds = 0;
		if (values.length == 3) {
			hour = Integer.parseInt(values[0]);
			minute = Integer.parseInt(values[1]);
			seconds = (long) Double.parseDouble(values[2]);
		} else if (values.length == 2) {
			minute = Integer.parseInt(values[0]);
			seconds = (long) Double.parseDouble(values[1]);
		} else if (values.length == 1) {
			seconds = (long) Double.parseDouble(values[0]);
		}
		StringBuffer buffer = new StringBuffer();
		if (hour == 0) {
			if (minute == 0) {
				buffer.append(seconds + "秒");
			} else {
				buffer.append(minute + "分" + seconds + "秒");
			}
		} else {
			buffer.append(hour + "时" + minute + "分" + seconds + "秒");
		}
		return buffer.toString();

	}
}
